package archimede;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;


public class PuntiRilevazione extends JTabbedPane {

	private JButton plus = new JButton(new ImageIcon(ClassLoader.getSystemResource("plus.png")));
	private int count = 0;
	private StartingWindow st;

	protected void paintComponent(Graphics g) {
		setOpaque(false);
		g.drawImage((new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg"))).getImage(), 0, 0, null);
		super.paintComponent(g);
	}

	public PuntiRilevazione(final StartingWindow st) {
		super();
		this.st = st;
		plus.setBorder(null);
		plus.setContentAreaFilled(false);
		plus.setRolloverEnabled(true);
		plus.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("plus_roll.png")));
		plus.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				addTab("M" + (++count) + "   ", new JScrollPane(new ObjM(st, count), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
						JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
					@Override
					public void setVerticalScrollBar(JScrollBar verticalScrollBar) {
						// TODO Auto-generated method stub
						verticalScrollBar.setUnitIncrement(15);
						super.setVerticalScrollBar(verticalScrollBar);
					}
				});
				setTabComponentAt(count, new ButtonTabObjM(PuntiRilevazione.this));
			}
		});
		addTab("", null);
		setTabComponentAt(count, plus);

	}

	public String print() {
		String print = "\n#" + count;
		for (int i = 1; i <= count; i++) {
			print = print + ((ObjM) ((JViewport) (((JScrollPane) getComponent(i)).getComponent(0))).getView()).print();
		}
		print = print + "\n@@Punti rilevazione";
		return print;
	}

	public boolean save() {
		boolean done = true;
		for (int i = 1; i <= count; i++) {
			if (!((ObjM) ((JViewport) (((JScrollPane) getComponent(i)).getComponent(0))).getView()).save())
				done = false;
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return done;
	}

	public void setElement(String data) {
		Scanner c = new Scanner(data);
		c.nextLine();
		count = Integer.parseInt(c.nextLine().substring(1));
		for (int i = 1; i <= count; i++) {
			ObjM tmp = new ObjM(st, i);
			String value = c.nextLine().substring(1);
			boolean next = true;
			while (c.hasNextLine() && next) {
				String line = c.nextLine();
				if (line.equalsIgnoreCase("#endm")) {
					tmp.setElement(value);
					next = false;
				}
				value = value + "\n" + line;
			}
			// System.out.println("MMMMMMM : "+i+"\n"+value);
			addTab("M" + (i), new JScrollPane(tmp, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) {
				@Override
				public void setVerticalScrollBar(JScrollBar verticalScrollBar) {
					// TODO Auto-generated method stub
					verticalScrollBar.setUnitIncrement(15);
					super.setVerticalScrollBar(verticalScrollBar);
				}
			});
			setTabComponentAt(i, new ButtonTabObjM(PuntiRilevazione.this));
		}
		setSelectedIndex(count);

	}

	public void replace(String newName, String oldName) {
		for (int i = 1; i <= count; i++) {
			((ObjM) ((JViewport) (((JScrollPane) getComponent(i)).getComponent(0))).getView()).replace(newName, oldName);
		}
	}

	public void setCount(int c, int rem) {
		for (int i = rem + 1; i <= count; i++) {
			((ObjM) ((JViewport) (((JScrollPane) getComponent(i)).getComponent(0))).getView()).changeM(i - 1);
		}
		count = c;
	}

	public int getCount() {
		return count;
	}
}
